﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_PageAdd : System.Web.UI.Page
{
    protected void Add_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            if (!ManagerLogin.ValidateRole(string.Format("0_{0}_d", this.ParentIdList.SelectedValue)))
            {
                this.Msg.CssClass = "errorMsg";
                this.Msg.Text = "您无此权限";
                this.Msg.Visible = true;
            }
            else
            {
                string[,] includeColumns = new string[,] { 
                    { "ParentId", this.ParentIdList.SelectedValue.ToString(), "int", "0" }, 
                    { "Title", this.PageTitle.Text, "NVarChar", "0" }, 
                    { "FileName", this.FileName.Text, "NVarChar", "32" }, 
                    { "PageContent", this.PageContent.Text.ToString(), "NText", "0" }, 
                    { "MenuId", (string)this.Session["MenuId"], "int", "0" }, 
                    { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } 
                };
                IDb db = new DbFactory().DriverDb();
                db.Open(); 
                db.Insert("SinglePage", includeColumns);
                db.Close();
                this.Msg.Text = "栏目添加成功！";
                this.Msg.Visible = true;
                this.PageTitle.Text = "";
                this.FileName.Text = "";
                this.PageContent.Text = "";
                this.Bind();
            }
        }
    }

    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            this.PageLink.NavigateUrl = "Page.aspx?menuId=" + str;
            this.MenuName.Text = str2;
            IDb db = new DbFactory().DriverDb();
            db.Open();
            DataTable oldTable = db.DataTable("Title,ParentId,SinglePageId", "SinglePage", string.Format("MenuId={0}", str), "OrderId ASC");
            db.Close();
            oldTable.Columns.Add("TitleText");
            DataTable newTable = oldTable.Clone();
            Function.PopulateTable(oldTable, newTable, "Title", "SinglePageId", "ParentId", 0, 0);
            this.ParentIdList.DataSource = newTable;
            this.ParentIdList.DataTextField = "TitleText";
            this.ParentIdList.DataValueField = "SinglePageId";
            this.ParentIdList.DataBind();
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count(FileName)", "SinglePage", string.Format("FileName='{0}'", args.Value.ToString()));
        db.Close();
        if (num > 0)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        this.Msg.CssClass = "msg";
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
}
